package com.lsh.oms.provider.service.order;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.alibaba.fastjson.JSON;
import com.lsh.base.common.model.CommonResult;
import com.lsh.oms.api.service.order.IOrderCreateRestService;
import com.lsh.oms.core.service.order.OrderCancelService;
import com.lsh.oms.core.service.order.OrderCreateService;
import com.lsh.oms.core.service.order.OrderQueryService;
import com.lsh.oms.core.service.order.OrderUpdateService;
import com.lsh.oms.provider.service.BaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;
import java.util.Map;

/**
 * @author peter
 */
@Service(protocol = "rest", timeout = 30000)
@Path("order")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
@Deprecated
public class OrderCreateRestServiceImpl extends BaseService implements IOrderCreateRestService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private OrderCreateService orderCreateService;

    @Autowired
    private OrderCancelService orderCancelService;

    @Autowired
    private OrderUpdateService orderUpdateService;

    @Autowired
    private OrderQueryService orderQueryService;

    /**
     *
     * @param orderCode
     * @return
     */
    @GET
    @Path("/tms/cancel")
    @Override
    public CommonResult<String> cancel(@QueryParam("orderCode") Long orderCode) {
        CommonResult commonResult = new CommonResult();
        try {
            if (orderCode == null || orderCode <= 0) {
                return CommonResult.error("【订单全单捡缺】 订单号不合法", "error");
            }
//            commonResult = orderCancelService.orderAllPickUpShortCancel(orderCode, "tms-cancel-order",this.getVenderId());
        } catch (Throwable e) {
            logger.error("【订单全单捡缺】订单取消失败",e);
            return CommonResult.error("【订单全单捡缺】订单取消失败", "error");
        }

        return commonResult;
    }

    /**
     * @param orderCode
     * @return
     */
    @GET
    @Path("/po/cancel")
    @Override
    public CommonResult<String> cancelPoOrder(@QueryParam("orderCode") Long orderCode) {

        CommonResult commonResult = CommonResult.success();
        try {
            if (orderCode == null || orderCode <= 0) {

                return CommonResult.error("【预下单订单取消】订单号不合法", "error");
            }

//            OrderHead orderHead = orderQueryService.findByCode(orderCode,false,false);
//
//            OrderHead filter = new OrderHead();
//            filter.setOrderCode(orderCode);
//
//            OrderHead entry = new OrderHead();
//            entry.setOrderStatus(OrderStatus.ORDER_COMMITING_OFC.getIndex());
//
//
//            entry.setExt(orderHead.getExt());
//            logger.info("拆单接口 修改数据库参数" + JSON.toJSONString(entry));
//            orderUpdateService.updateOrderHead(entry, filter);

            commonResult = orderCancelService.orderCancel(orderCode, "po-order-cancel-order");

        } catch (Throwable e) {
            logger.error("【预下单订单取消】订单取消失败",e);
            return CommonResult.error("【预下单订单取消】订单取消失败", "error");
        }

        return commonResult;
    }

    /**
     * 创建确认
     *
     * @param codes
     * @return
     */
    @POST
    @Path("/create/confirm")
    @Override
    public CommonResult<Map<Long, Integer>> confirm(List<Long> codes) {

        Map<Long, Integer> ret = this.orderCreateService.enable(codes);
        logger.info("confirm codes is {}", JSON.toJSONString(codes));
        return new CommonResult<>("100000", "success", ret);
    }
}
